package com.sansi.pencilbook.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Book;
import com.sansi.pencilbook.entity.Category;
import com.sansi.pencilbook.entity.UseBook;
import com.sansi.pencilbook.mapper.UseBookMapper;
import com.sansi.pencilbook.service.CategoryServer;
import com.sansi.pencilbook.service.UseBookServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明:
 *
 * @author lsy13
 */
@Service
public class UseBookServerImpl implements UseBookServer {

    @Autowired
    private UseBookMapper useBookMapper;

    @Autowired
    private CategoryServer categoryServer;

    @Override
    public Result getBooksAll() {
        try {
            List<UseBook> books = useBookMapper.findAll();
            return books.size()>0 ? Result.ofSuccess("查询成功", books.size(), books) :
                    Result.ofFailed("目前不存在书籍");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getBooksAll(int pageNum, int pageSize) {
        Page<UseBook> page = PageHelper.startPage(pageNum, pageSize);
        try {
            useBookMapper.findAll();
            return page.getTotal()>0 ? Result.ofSuccess("查询成功", page.size(), page.getResult()) :
                    Result.ofFailed("查询结果为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getBooksByCategory(int categoryId) {
        List<UseBook> list = null;
        try {
            List<Category> categories = (List<Category>) categoryServer.getCategoriesByParent(categoryId).getData();
            list = useBookMapper.findByCategoryId(categoryId);
            if (categories != null) {
                for (Category category : categories) {
                    list.addAll(useBookMapper.findByCategoryId(category.getCategoryId()));
                }
            }
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("该类别目前不存在书籍");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getBooksByName(String name) {
        try {
            List<UseBook> books = useBookMapper.findByName(name);
            return books.size()>0 ? Result.ofSuccess("查询成功", books.size(), books) :
                    Result.ofFailed("书籍不存在");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getBooksByName(String name, int pageNum, int pageSize) {
        Page<UseBook> page = PageHelper.startPage(pageNum, pageSize);
        try {
            useBookMapper.findByName(name);
            return page.getTotal()>0 ? Result.ofSuccess("查询成功", page.size(), page.getResult()) :
                    Result.ofFailed("查询结果为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getBookByIsbn(String isbn) {
        try {
            UseBook book = useBookMapper.findByISBN(isbn);
            return book!=null ? Result.ofSuccess("查询成功", 1, book) :
                    Result.ofFailed("书籍不存在");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getBooksByAuthor(String author) {
        try {
            List<UseBook> books = useBookMapper.findByAuthor(author);
            return books.size()>0 ? Result.ofSuccess("查询成功", books.size(), books) :
                    Result.ofFailed("查询不到该作者的相关书籍");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getBooksByAuthor(String author, int pageNum, int pageSize) {
        Page<UseBook> page = PageHelper.startPage(pageNum, pageSize);
        try {
            useBookMapper.findByAuthor(author);
            return page.getTotal()>0 ? Result.ofSuccess("查询成功", page.size(), page.getResult()) :
                    Result.ofFailed("查询结果为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getBooksByPublisher(String publisher) {
        try {
            List<UseBook> books = useBookMapper.findByPublisher(publisher);
            return books.size()>0 ? Result.ofSuccess("查询成功", books.size(), books) :
                    Result.ofFailed("查询不到该出版社的书籍");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getBooksByPublisher(String publisher, int pageNum, int pageSize) {
        Page<UseBook> page = PageHelper.startPage(pageNum, pageSize);
        try {
            useBookMapper.findByPublisher(publisher);
            return page.getTotal()>0 ? Result.ofSuccess("查询成功", page.size(), page.getResult()) :
                    Result.ofFailed("查询结果为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getBooksByParam(String param) {
        try {
            List<UseBook> list = (List<UseBook>) useBookMapper.findByParam(param);
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }

    @Override
    public Result getBooksByCategory(int categoryId, int pageNum, int pageSize) {
        Page<UseBook> page = PageHelper.startPage(pageNum, pageSize);
        try {
            useBookMapper.findByCategoryId(categoryId);
            return page.getTotal()>0 ? Result.ofSuccess("查询成功", page.size(), page.getResult()) :
                    Result.ofFailed("查询结果为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }
}
